package com.mapbox.navigation.core.routerefresh

import com.mapbox.navigation.base.route.NavigationRoute

internal object RouteRefreshValidator {

    fun joinValidationErrorMessages(
        validations: List<Pair<RouteValidationResult, NavigationRoute>>
    ): String = validations.mapNotNull { pair ->
        (pair.first as? RouteValidationResult.Invalid)?.let { invalidResult ->
            "${pair.second.id} ${invalidResult.reason}"
        }
    }.joinToString(separator = ". ")

    fun validateRoute(route: NavigationRoute): RouteValidationResult = when {
        route.routeOptions.enableRefresh() != true ->
            RouteValidationResult.Invalid("RouteOptions#enableRefresh is false")
        route.directionsRoute.requestUuid()?.isNotBlank() != true ->
            RouteValidationResult.Invalid(
                "DirectionsRoute#requestUuid is blank. " +
                    "This can be caused by a route being generated by " +
                    "an Onboard router (in offline mode). " +
                    "Make sure to switch to an Offboard route when possible, " +
                    "only Offboard routes support the refresh feature."
            )
        else -> RouteValidationResult.Valid
    }

    sealed class RouteValidationResult {
        object Valid : RouteValidationResult()
        data class Invalid(val reason: String) : RouteValidationResult()
    }
}
